package com.jfinaldemo.config;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.PostgreSqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.render.ViewType;
import com.jfinaldemo.controller.GetParameterController;
import com.jfinaldemo.controller.HelloWorldController;
import com.jfinaldemo.controller.InterceptorController;
import com.jfinaldemo.controller.TestController;
import com.jfinaldemo.controller.UploadController;
import com.jfinaldemo.controller.UserController;
import com.jfinaldemo.interceptor.ActionTestInterceptor;
import com.jfinaldemo.interceptor.GlobalTestInterceptor;
import com.jfinaldemo.model.User;

/**
 * 配置类
 * @author huchengsong
 *
 */
public class Config extends JFinalConfig {

//此方法用来配置 JFinal 常量值
	@Override
	public void configConstant(Constants me) {
		
		loadPropertyFile("a_little_config.txt");	//加载数据库配置信息                
	    me.setDevMode(getPropertyToBoolean("devMode", true)); //开发模式常量 devMode 的配置
//	    me.setViewType(ViewType.FREE_MARKER);
//	    me.setBaseViewPath("WEB-INF/view");
	    me.setViewType(ViewType.JSP);//默认视图类型为 JSP
//	    me.setUploadedFileSaveDirectory("D:\\apache-tomcat-6.0.18\\webapps\\jfinaldemo_upload");
	    me.setUploadedFileSaveDirectory("D:\\declaimer");
//	    me.setBaseDownloadPath("D:\\declaimer");
	    
	}

	
	//此方法用来配置 JFinal 访问路由 
	@Override
	public void configRoute(Routes me) {
		
		me.add("/",HelloWorldController.class);
		me.add("/param",GetParameterController.class);
		me.add("/user",UserController.class);
		me.add("/interceptor",InterceptorController.class);
		me.add("/upload",UploadController.class);
		me.add("/te",TestController.class);
	}

	@Override
	public void configPlugin(Plugins me) {
		
		String decPassWord =getProperty("password");
		String decUser = getProperty("user");
		DruidPlugin druid = new DruidPlugin(getProperty("jdbcUrl"),decUser, decPassWord);
	    me.add(druid); //添加连接池 
	    ActiveRecordPlugin arp = new ActiveRecordPlugin(druid);
	    me.add(arp); //接入ActiveRecord插件   ActiveRecord是数据库访问插件
	    arp.setShowSql(true);//调试阶段，查看SQL语句
	    arp.addMapping("t_user", User.class);//将实体和表映射
//	    arp.setDialect(new PostgreSqlDialect());//Postgresql方言
	    
	}

	@Override
	public void configInterceptor(Interceptors me) {
		
		me.add(new GlobalTestInterceptor());//定义一个全局拦截器
//		me.add(new ActionTestInterceptor());//定义一个Action级拦截器
		
	}

	@Override
	public void configHandler(Handlers me) {
		
		 me.add(new ContextPathHandler("contextPath"));//设置上下文路径
	}

}
